import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)


import Layout from '@/layout'



  const  systemRoutes=  {
  path:'/system',
  component: Layout,
  meta: { title: '系统管理', icon: 'el-icon-monitor' },
  children: [
    {
      path:'config',
      component:()=>import('@/views/system/config'),
      meta: { title: '系统配置', icon: 'el-icon-monitor' }
    }
  ]
}
  const  adminRoutes=  {
  path:'/admin',
  component: Layout,
  meta: { title: '权限管理', icon: 'el-icon-monitor' },
  children: [
    {
      path:'index',
      component:()=>import('@/views/admin/index'),
      meta: { title: '操作员管理', icon: 'el-icon-monitor' }
    },
    {
      path:'role',
      component:()=>import('@/views/admin/role'),
      meta: { title: '角色管理', icon: 'el-icon-monitor' }
    },
    {
      path:'modifyPwd',
      component:()=>import('@/views/admin/modifyPwd'),
      meta: { title: '修改密码', icon: 'el-icon-monitor' }
    }
  ]
}
  const basicRoutes={
    path:'/basic',
    component: Layout,
    meta: { title: '基础功能', icon: 'el-icon-monitor' },
    children: [
      {
        path:'category',
        component:()=>import('@/views/basic/category'),
        meta: { title: '文章分类', icon: 'el-icon-monitor' }
      },
      {
        path:'article',
        component:()=>import('@/views/basic/article'),
        meta: { title: '文章管理', icon: 'el-icon-monitor' }
      },
      {
        path:'edit_article',
        component:()=>import('@/views/basic/edit_article'),
        meta: { title: '添加修改文章', icon: 'el-icon-monitor' },
        hidden:true
      }
    ]
  }
  const designRoutes={
    path:'/design',
    component: Layout,
    meta: { title: '页面装修', icon: 'el-icon-monitor' },
    children: [
      {
        path:'page',
        component:()=>import('@/views/design/page'),
        meta: { title: '页面管理', icon: 'el-icon-monitor' }
      },
      {
        path:'page-detail',
        component:()=>import('@/views/design/page-detail'),
        meta: { title: '页面详细', icon: 'el-icon-monitor' },
        hidden:true
      },
      {
        path:'module',
        component:()=>import('@/views/design/module'),
        meta: { title: '组件管理', icon: 'el-icon-monitor' }
      },
      {
        path:'module-detail',
        component:()=>import('@/views/design/module-detail'),
        meta: { title: '组件详细', icon: 'el-icon-monitor' },
        hidden:true
      }
    ]
  }

  const userRoutes= {
    path: '/user',
    component: Layout,
    meta: {title: '用户管理', icon: 'el-icon-monitor'},
    children: [
      {
        path: 'index',
        component: () => import('@/views/user/index'),
        meta: {title: '用户列表', icon: 'el-icon-monitor'}
      }
    ]
  }
export const roleRoutes=[
    {
      path: '*',
      redirect: '/401',
      hidden: true
    },
    systemRoutes,
    adminRoutes,
    basicRoutes,
    designRoutes,
    userRoutes
]

export const constantRoutes = [
  {
    path: '/',
    component: Layout,
    redirect: '/dashboard',
    name: '/home',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '面板', icon: 'el-icon-monitor' }
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    children: [
      {
        path: '401',
        name: 'Dashboard',
        component: () => import('@/views/401'),
        meta: { title: '权限不足', icon: 'el-icon-monitor' }
      }
    ],
    hidden: true
  }
]

const createRouter = () =>
  new Router({
    // mode: 'history', // require service support
    // scrollBehavior: () => ({ y: 0 }),
    routes: constantRoutes
  })

const router = createRouter()
export default router

export function permissionMenu(menuIds){
  let array=handlerMenu(roleRoutes,menuIds);
  return array;
}
function handlerMenu(list,menuIds) {
  let array = [];
  for (let i = 0; i < list.length; i++) {
    let d = list[i];
    if (d.meta&&menuIds.includes(d.meta.mid)) {
      if (d.children) {
        let child = handlerMenu(d.children, menuIds)
        d['children'] = child;
      }
      array.push(d)
    }
  }
  return array;
}





